package com.ruoyi.subject.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.subject.domain.QbSubWrong;
import com.ruoyi.subject.mapper.QbSubWrongMapper;
import com.ruoyi.subject.service.IQbSubWrongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 我的错题Service业务层处理
 *
 * @author lzhj
 * @date 2021-01-27
 */
@Service
public class QbSubWrongServiceImpl implements IQbSubWrongService {
    @Autowired
    private QbSubWrongMapper qbSubWrongMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询我的错题
     *
     * @param id 我的错题ID
     * @return 我的错题
     */
    @Override
    public QbSubWrong selectQbSubWrongById(Long id) {
        return qbSubWrongMapper.selectQbSubWrongById(id);
    }

    /**
     * 查询我的错题列表
     *
     * @param qbSubWrong 我的错题
     * @return 我的错题
     */
    @Override
    public List<QbSubWrong> selectQbSubWrongList(QbSubWrong qbSubWrong) {
        return qbSubWrongMapper.selectQbSubWrongList(qbSubWrong);
    }

    /**
     * 新增我的错题
     *
     * @param qbSubWrong 我的错题
     * @return 结果
     */
    @Override
    public int insertQbSubWrong(QbSubWrong qbSubWrong) {
        qbSubWrong.setCreateTime(DateUtils.getNowDate());
        return qbSubWrongMapper.insertQbSubWrong(qbSubWrong);
    }

    /**
     * 修改我的错题
     *
     * @param qbSubWrong 我的错题
     * @return 结果
     */
    @Override
    public int updateQbSubWrong(QbSubWrong qbSubWrong) {
        qbSubWrong.setUpdateTime(DateUtils.getNowDate());
        return qbSubWrongMapper.updateQbSubWrong(qbSubWrong);
    }

    /**
     * 批量删除我的错题
     *
     * @param ids 需要删除的我的错题ID
     * @return 结果
     */
    @Override
    public int deleteQbSubWrongByIds(Long[] ids) {
        return qbSubWrongMapper.deleteQbSubWrongByIds(ids);
    }

    /**
     * 删除我的错题信息
     *
     * @param id 我的错题ID
     * @return 结果
     */
    @Override
    public int deleteQbSubWrongById(Long id) {
        return qbSubWrongMapper.deleteQbSubWrongById(id);
    }

    @Override
    public List<QbSubWrong> selectQbSubWrongListByUserId(Long userId) {
        return qbSubWrongMapper.selectQbSubWrongListByUserId(userId);
    }

    @Override
    public boolean selectQbSubWrongBy(QbSubWrong qbSubWrong) {
        String sql = "select count(1) from t_qb_sub_wrong where user_id=? and subject_id=?";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class, qbSubWrong.getUserId(), qbSubWrong.getSubjectId());
        return integer > 0 ? true : false;
    }
}
